package com.kkd.kkdapp.adapter;

import java.util.ArrayList;
import java.util.List;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.KKDWebActivity;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class IconImagePagerAdapter extends PagerAdapter {
	private String[] images;
	private Context context;
	private Dialog dialog;
	private String[] text;
	private List<View> viewList;

	public IconImagePagerAdapter(Context context, String[] images,
			String[] text, Dialog dialog) {
		this.context = context;
		this.images = images;
		this.dialog = dialog;
		this.text = text;
		viewList = new ArrayList<View>();
		for (int i = 0; i < images.length; i++) {
			View view = LayoutInflater.from(context).inflate(
					R.layout.viewpagerpopup, null);
			viewList.add(view);
		}
	}

	@Override
	public int getCount() {
		return images.length;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	public void destroyItem(android.view.ViewGroup container, int position,
			Object object) {
		// container.removeView((View) object);
	}

	@SuppressLint("ResourceAsColor")
	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		View view = viewList.get(position);
		ImageView img = (ImageView) view.findViewById(R.id.img);
		TextView textview = (TextView) view.findViewById(R.id.textview);
		textview.setText(text[position]);
		RelativeLayout layout = (RelativeLayout) view.findViewById(R.id.layout);
		ImageLoader.getInstance().displayImage(images[position], img);
		if (images[position] == images[images.length - 1]) {
			layout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, KKDWebActivity.class);
					intent.putExtra("url",
							com.kkd.kkdapp.Environment.getH5Url()
									+ "appinsurance.html"); // 正式
					intent.putExtra("titleName", "安全保障");
					context.startActivity(intent);
					if (dialog != null) {
						dialog.cancel();
						dialog = null;
					}
				}
			});
		}
		ViewParent vp = view.getParent();
		if (vp != null) {
			ViewGroup parent = (ViewGroup) vp;
			parent.removeView(view);
		}
		container.addView(view);
		return view;
	}
}
